package com.studyyoun.android.nonghe.home.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidlongs.pullrefreshrecyclerylib.common.PullRecyclerViewUtils;
import com.androidlongs.pullrefreshrecyclerylib.inter.PullRecyclerViewLinserner;
import com.androidlongs.pullrefreshrecyclerylib.inter.PullRecyclerViewOnItemClickLinserner;
import com.androidlongs.pullrefreshrecyclerylib.model.PullRecyclerMoreStatueModel;
import com.studyyoun.android.androidcommonbaselibrary.execut.ExecutorFunction;
import com.studyyoun.android.androidcommonbaselibrary.fragment.CommonBaseFragment;
import com.studyyoun.android.androidcommonbaselibrary.glide.CommonGlideUtils;
import com.studyyoun.android.androidcommonbaselibrary.utils.DateUtils;
import com.studyyoun.android.androidcommonbaselibrary.utils.LogUtils;
import com.studyyoun.android.nonghe.R;
import com.studyyoun.android.nonghe.home.activity.ArtDetaileActivity;
import com.studyyoun.android.nonghe.home.activity.VideoDetaileActivity;
import com.studyyoun.android.nonghe.home.http.HomeHttpManager;
import com.studyyoun.android.nonghe.home.model.ArtModel;
import com.studyyoun.android.nonghe.home.model.CommonSearcherModel;
import com.studyyoun.android.nonghe.home.model.VideoModel;
import com.studyyoun.android.nonghe.home.sql.ArtSqlDBHelper;
import com.studyyoun.android.nonghe.http.HttpCallBackHelper;
import com.studyyoun.android.nonghe.http.HttpUrlHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by androidlongs on 2017/8/27.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

public class HomeItemFragment extends CommonBaseFragment {

    private RelativeLayout mMainContentLayout;
    private PullRecyclerViewUtils pullRecyclerViewUtils;
    private ArtSqlDBHelper mArtSqlDBHelper;
    private String mPageTape = "android";

    private static LinearLayout mRootLinearLayout;


    public HomeItemFragment() {

    }


    @Override
    protected int getCommonLayoutId() {
        return R.layout.fragment_home_item_layout;
    }

    @Override
    protected void commonInitView(View view) {
        mMainContentLayout = view.findViewById(R.id.ll_home_item_content);
        mRootLinearLayout = view.findViewById(R.id.ll_art_item_root_view);

        //初始化
        pullRecyclerViewUtils = PullRecyclerViewUtils.getInstance();

        Bundle bundle = getArguments();
        //判断需写
        if (bundle != null) {
            mPageTape = bundle.getString("pageType");
            mType = bundle.getInt("pageIndex");
        }

        //3初始化Recyclerview
        /**
         * 参数一 this Context实例
         * 参数二 单一布局模式条目布局ID
         * 参数三 数据集合
         * 参数四 回调监听
         */
        RelativeLayout relativeLayout = pullRecyclerViewUtils.setRecyclerViewFunction(mContext, -1, mCommonArtModelList, mPullRecclerLinserner, mItemClickLinserner);

        //4相关设置

        //下拉刷新和上拉加载更多模式
        pullRecyclerViewUtils.setRecyclerviewStatue(PullRecyclerViewUtils.RECYCLRYVIEW_STATUE.PULL_AND_UP);

        pullRecyclerViewUtils.setFirstDefaultPageType(PullRecyclerViewUtils.SHOW_DEFAUTLE_PAGE_TYPE.LOADING);

        pullRecyclerViewUtils.setPullScrollRecyclerLiserner(mOnPullRecyclerviewScrollLiserner);
        //5将recyclerview添加到当前显示的页面中
        mMainContentLayout.addView(relativeLayout);


    }

    private PullRecyclerViewUtils.OnPullRecyclerviewScrollLiserner mOnPullRecyclerviewScrollLiserner = new PullRecyclerViewUtils.OnPullRecyclerviewScrollLiserner() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy, LinearLayoutManager mLinearLayoutManager) {

            LogUtils.d("scroll dy " + dy);

        }
    };

    private NetLoadingType mNetLoadingType = NetLoadingType.DEFAULTlOADING;


    private enum NetLoadingType {
        UP_LOADING,
        PULL_LOADING,
        DEFAULTlOADING
    }

    @Override
    protected void commonFunction() {
        mArtSqlDBHelper = new ArtSqlDBHelper(mContext);
    }

    @Override
    protected void commonDelayFunction() {
        if (mType == 0) {
            loadingNetDatas(0);
        }
    }

    private PullRecyclerViewOnItemClickLinserner mItemClickLinserner = new PullRecyclerViewOnItemClickLinserner() {
        @Override
        public void setonItemClick(int position, int itemType, Object object) {
            PullRecyclerMoreStatueModel model = (PullRecyclerMoreStatueModel) object;
            itemType = model.itemType;
            if (itemType == 1 || itemType == 4 || itemType == 6) {

                ArtModel artModel = (ArtModel) model.model;

                Intent intent = new Intent(HomeItemFragment.this.getActivity(), ArtDetaileActivity.class);
                intent.putExtra("artModel", artModel);
                HomeItemFragment.this.startActivity(intent);
            } else if (itemType == 2 || itemType == 3) {
                VideoModel videoModel = (VideoModel) model.model;

                Intent intent = new Intent(HomeItemFragment.this.getActivity(), VideoDetaileActivity.class);
                intent.putExtra("videoModel", videoModel);
                HomeItemFragment.this.startActivity(intent);
            }
        }
    };
    /**
     * RecyclerView相关操作的回调
     */
    private PullRecyclerViewLinserner mPullRecclerLinserner = new PullRecyclerViewLinserner() {
        @Override
        public void loadMoreData() {

            mCurrentIndex++;
            mNetLoadingType = NetLoadingType.UP_LOADING;
            loadNet(mType);
        }

        @Override
        public void loadingRefresDataFunction() {
            mCurrentIndex = 0;
            mArtModelList.clear();
            mCommonArtModelList.clear();
            mNetLoadingType = NetLoadingType.PULL_LOADING;
            loadNet(mType);
        }

        @Override
        public void setViewDatas(View itemView, int position, int itemType, Object object) {

            PullRecyclerMoreStatueModel pullRecyclerMoreStatueModel = (PullRecyclerMoreStatueModel) object;
            itemType = pullRecyclerMoreStatueModel.itemType;

            if (itemType == 1 || itemType == 4 || itemType == 6) {
                ArtModel artModel = (ArtModel) pullRecyclerMoreStatueModel.model;

                String artName = artModel.artName;
                TextView titleTextView = itemView.findViewById(R.id.tv_home_item_art_title);
                titleTextView.setText(artName);

                String imageUrl = artModel.artImageUrl;


                final String artFalg = artModel.artFalg;

                if (itemType == 6) {
                    RecyclerView picRecyclerView = itemView.findViewById(R.id.rv_home_item_art_img);
                    int index = 3;
                    float height = 0;
                    float imageViewHeight = 0;
                    final List<String> artImageUrlList = artModel.artImageUrlList;
                    if (artImageUrlList.size() % 2 == 0 && artImageUrlList.size() % 3 != 0) {
                        index = 2;
                        height = artImageUrlList.size() / 2 * 94 * mScaledDensity + 2 * mScaledDensity * (artImageUrlList.size() / 2 + 1);
                        ViewGroup.LayoutParams layoutParams = picRecyclerView.getLayoutParams();
                        layoutParams.height = (int) height;
                        picRecyclerView.setLayoutParams(layoutParams);

                        imageViewHeight = 94 * mScaledDensity;

                    } else {
                        index = 3;
                        height = artImageUrlList.size() / 3 * 76 * mScaledDensity + 2 * mScaledDensity * (artImageUrlList.size() / 2 + 1);
                        imageViewHeight = 76 * mScaledDensity;
                        if (artImageUrlList.size() % 3 > 0) {
                            height += 76 * mScaledDensity + 2 * mScaledDensity;
                        }
                        ViewGroup.LayoutParams layoutParams = picRecyclerView.getLayoutParams();
                        layoutParams.height = (int) height;
                        picRecyclerView.setLayoutParams(layoutParams);
                    }


                    //设置布局模式
                    GridLayoutManager layoutManager = new GridLayoutManager(mContext, index);
                    //设置滚动模式为竖直方向
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    //设置
                    picRecyclerView.setLayoutManager(layoutManager);

                    final float finalHeight = imageViewHeight;
                    final int finalIndex = index;
                    picRecyclerView.setAdapter(new RecyclerView.Adapter<RecyclerView.ViewHolder>() {
                        @Override
                        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                            View view = View.inflate(mContext, R.layout.item_art_pic_layout, null);
                            return new PicViewHolder(view, mContext, finalHeight, mScaledDensity);
                        }

                        @Override
                        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

                            PicViewHolder picViewHolder = (PicViewHolder) holder;
                            picViewHolder.setDatasFunction(artImageUrlList.get(position), artImageUrlList, finalIndex, position, artFalg);
                        }

                        @Override
                        public int getItemCount() {
                            return artImageUrlList.size();
                        }
                    });

                } else {
                    ImageView imageView = itemView.findViewById(R.id.iv_home_item_art_img);
                    imageUrl = HttpUrlHelper.BaseImageHost + imageUrl;
                    CommonGlideUtils.showImageView(mContext, R.mipmap.default_pic, imageUrl, imageView);

                }


                TextView falgTextView = itemView.findViewById(R.id.tv_home_item_art_flg);
                falgTextView.setText(artFalg);
                if (itemType == 6) {
                    falgTextView.setVisibility(View.GONE);
                } else {
                    falgTextView.setVisibility(View.VISIBLE);
                }


                String author = artModel.author;
                TextView authorTextView = itemView.findViewById(R.id.tv_home_item_art_author);
                authorTextView.setText(author);


                String artReadCount = artModel.artReadCount;
                TextView artReadCountTextView = itemView.findViewById(R.id.tv_home_item_art_read);
                artReadCountTextView.setText(artReadCount + "阅读");

                List<String> articlPariseModels = artModel.articlPariseModels;
                String artParise = "0";
                if (articlPariseModels != null) {
                    artParise = String.valueOf(articlPariseModels.size());
                }
                TextView artPariseCountTextView = itemView.findViewById(R.id.tv_home_item_art_pra);

                artPariseCountTextView.setText("• " + artParise + "赞");


                String artCreateTime = artModel.artCreateTime;
                TextView artCreateTimeTextView = itemView.findViewById(R.id.tv_home_item_art_date);


                artCreateTimeTextView.setText("• 创建分享于 " + DateUtils.getInstance().formatTime(Long.valueOf(artCreateTime), "yyyy年MM月dd日"));


                TextView artIsTopTextView = itemView.findViewById(R.id.tv_home_item_art_is_top);
                TextView artIsRecommendTextView = itemView.findViewById(R.id.tv_home_item_art_is_recommend);

                switch (mType) {
                    case 0:
                        if (artModel.artIsTop == 1) {
                            artIsTopTextView.setVisibility(View.VISIBLE);
                        } else {
                            artIsTopTextView.setVisibility(View.GONE);
                        }

                        if (artModel.artIsRecommend == 1) {
                            artIsRecommendTextView.setVisibility(View.VISIBLE);
                        } else {
                            artIsRecommendTextView.setVisibility(View.GONE);
                        }

                        break;
                    case 1:
                    case 2:
                        artIsTopTextView.setVisibility(View.GONE);
                        artIsRecommendTextView.setVisibility(View.GONE);
                        break;
                    default:
                        artIsTopTextView.setVisibility(View.GONE);
                        artIsRecommendTextView.setVisibility(View.GONE);
                        break;
                }

            } else if (itemType == 2 || itemType == 3) {
                VideoModel videoModel = (VideoModel) pullRecyclerMoreStatueModel.model;

                String artName = videoModel.getTitle().trim();
                TextView titleTextView = itemView.findViewById(R.id.tv_home_item_art_title);
                titleTextView.setText(artName);

                String imageUrl = videoModel.getImageUrl();
                ImageView imageView = itemView.findViewById(R.id.iv_home_item_art_img);
                imageUrl = HttpUrlHelper.BaseImageHost + imageUrl;
                CommonGlideUtils.showImageView(mContext, R.mipmap.default_pic, imageUrl, imageView);


                String artFalg = videoModel.getFalg();
                TextView falgTextView = itemView.findViewById(R.id.tv_home_item_art_flg);
                falgTextView.setText(artFalg);


                String author = "代码小狂人";
                TextView authorTextView = itemView.findViewById(R.id.tv_home_item_art_author);
                authorTextView.setText(author);


                String artReadCount = String.valueOf(videoModel.readCount);
                TextView artReadCountTextView = itemView.findViewById(R.id.tv_home_item_art_read);
                artReadCountTextView.setText(artReadCount + "");


                List<String> articlPariseModels = videoModel.getPariseUserIdList();
                String artParise = "0";
                if (articlPariseModels != null) {
                    artParise = String.valueOf(articlPariseModels.size());
                }
                TextView artPariseCountTextView = itemView.findViewById(R.id.tv_home_item_art_pra);
                artPariseCountTextView.setText(artParise);


                String artCreateTime = videoModel.getCreatTime();
                TextView artCreateTimeTextView = itemView.findViewById(R.id.tv_home_item_art_date);
                artCreateTimeTextView.setText(DateUtils.getInstance().formatTime(Long.valueOf(artCreateTime), "yyyy-MM-dd"));

                TextView artIsTopTextView = itemView.findViewById(R.id.tv_home_item_art_is_top);
                TextView artIsRecommendTextView = itemView.findViewById(R.id.tv_home_item_art_is_recommend);

                switch (mType) {
                    case 0:
                        if (videoModel.getVideoIsTop() == 1) {
                            artIsTopTextView.setVisibility(View.VISIBLE);
                        } else {
                            artIsTopTextView.setVisibility(View.GONE);
                        }

                        if (videoModel.getVideoIsRecommend() == 1) {
                            artIsRecommendTextView.setVisibility(View.VISIBLE);
                        } else {
                            artIsRecommendTextView.setVisibility(View.GONE);
                        }

                        break;
                    case 1:
                    case 2:
                        artIsTopTextView.setVisibility(View.GONE);
                        artIsRecommendTextView.setVisibility(View.GONE);
                        break;
                    default:
                        artIsTopTextView.setVisibility(View.GONE);
                        artIsRecommendTextView.setVisibility(View.GONE);
                        break;
                }
            }

        }
    };

    private static class PicViewHolder extends RecyclerView.ViewHolder {

        private Context mContext;
        private final ImageView mImageView;
        private float mViewHeight;
        private float mScaledDensity;

        public PicViewHolder(View itemView, Context context, float finalHeight, float scaledDensity) {
            super(itemView);
            mContext = context;
            mImageView = itemView.findViewById(R.id.iv_item_art_pic);
            mViewHeight = finalHeight;
            mScaledDensity = scaledDensity;
        }

        public void setDatasFunction(String s, final List<String> artImageUrlList, final int finalIndex, final int position, final String artFalg) {

            LogUtils.d("设置图片 " + HttpUrlHelper.BaseImageHost + s);


            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mImageView.getLayoutParams();
            layoutParams.height = (int) mViewHeight;
            if (finalIndex == 2) {
                int flag = (int) (2 * mScaledDensity);
                layoutParams.setMargins(flag, flag, flag, flag);
            } else if (finalIndex == 3) {
                int flag = (int) (2 * mScaledDensity);
                layoutParams.setMargins(flag, flag, flag, flag);
            }

            mImageView.setLayoutParams(layoutParams);

            CommonGlideUtils.showImageView(mContext, R.mipmap.default_pic, HttpUrlHelper.BaseImageHost + s, mImageView);

            mImageView.setOnClickListener(new View.OnClickListener() {
                private boolean isStartAnimation = false;

                @Override
                public void onClick(View view) {
                    if (!isStartAnimation) {

                        ScaleAnimation scaleAnimation = new ScaleAnimation(0.92f, 1f, 0.92f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                        scaleAnimation.setDuration(200);
                        scaleAnimation.setInterpolator(new OvershootInterpolator());
                        scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {
                                isStartAnimation = true;
                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                LogUtils.d("scale animation close ");
                                isStartAnimation = false;
                                showPopPicFunction(mImageView, position, artImageUrlList, artFalg);
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {

                            }
                        });
                        mImageView.setAnimation(scaleAnimation);
                        mImageView.startAnimation(scaleAnimation);

                    }
                }
            });

        }

        private PopupWindow mShowPicPopupWindow;

        public void showPopPicFunction(final ImageView imageView, final int pos, final List<String> list, String artFlag) {

            if (mShowPicPopupWindow != null) {
                mShowPicPopupWindow.dismiss();
                mShowPicPopupWindow = null;
                return;
            }

            Intent hidStatue = new Intent("hidd_statue");
            mContext.sendBroadcast(hidStatue);

            // 获取自定义布局文件activity_popupwindow_left.xml的视图
            final View popupWindow_view = View.inflate(imageView.getContext(), R.layout.item_show_pic_list_layout, null);

            // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
            mShowPicPopupWindow = new PopupWindow(popupWindow_view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            //焦点设置
            mShowPicPopupWindow.setFocusable(true);

            //设置点击背景不消失
            mShowPicPopupWindow.setOutsideTouchable(false);
            // 设置透明背景
            mShowPicPopupWindow.setBackgroundDrawable(new ColorDrawable(
                    Color.TRANSPARENT));
            //设置动画

            mShowPicPopupWindow.setAnimationStyle(com.studyyoun.android.androidcommonbaselibrary.R.style.popwin_from_center_anim_style);

            mShowPicPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    Intent hidStatue = new Intent("show_statue");
                    mContext.sendBroadcast(hidStatue);
                }
            });
            //显示
            mShowPicPopupWindow.showAtLocation(mImageView, Gravity.NO_GRAVITY, 0, 0);

            final TextView artRemarkTextView = popupWindow_view.findViewById(R.id.tv_art_show_remark);
            artRemarkTextView.setText(artFlag);
            ViewPager viewPager = popupWindow_view.findViewById(R.id.vp_item_art_item_show_pic);
            viewPager.setAdapter(new PagerAdapter() {


                // 获取要滑动的控件的数量，在这里我们以滑动的广告栏为例，那么这里就应该是展示的广告图片的ImageView数量
                @Override
                public int getCount() {
                    return list.size() + 1;
                }

                // 来判断显示的是否是同一张图片，这里我们将两个参数相比较返回即可
                @Override
                public boolean isViewFromObject(View arg0, Object arg1) {
                    return arg0 == arg1;
                }

                // PagerAdapter只缓存三张要显示的图片，如果滑动的图片超出了缓存的范围，就会调用这个方法，将图片销毁
                @Override
                public void destroyItem(ViewGroup view, int position, Object object) {
                    view.removeView((View) object);
                }

                // 当要显示的图片可以进行缓存的时候，会调用这个方法进行显示图片的初始化，我们将要显示的ImageView加入到ViewGroup中，然后作为返回值返回即可
                @Override
                public Object instantiateItem(ViewGroup view, int position) {
                    View iteview = View.inflate(mContext, R.layout.item_art_show_pic_layout, null);
                    view.addView(iteview);
                    if (position == list.size()) {
                        ImageView imageView1 = iteview.findViewById(R.id.iv_item_art_pic);
                        imageView1.setVisibility(View.INVISIBLE);
                    } else {

                        ImageView imageView1 = iteview.findViewById(R.id.iv_item_art_pic);
                        imageView1.setVisibility(View.VISIBLE);
                        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView1.getLayoutParams();
                        layoutParams.width = mContext.getResources().getDisplayMetrics().widthPixels;
                        layoutParams.height = (int) (mContext.getResources().getDisplayMetrics().heightPixels - 27 * mContext.getResources().getDisplayMetrics().scaledDensity);
                        imageView1.setLayoutParams(layoutParams);
                        CommonGlideUtils.showImageView(mContext, R.mipmap.default_pic, HttpUrlHelper.BaseImageHost + list.get(position), imageView1);
                    }
                    return iteview;

                }
            });

            viewPager.setOffscreenPageLimit(list.size());
            viewPager.setCurrentItem(pos);


            final TextView currentIndexTextView = popupWindow_view.findViewById(R.id.tv_current_index);
            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {

                    currentIndexTextView.setText((position + 1) + "/" + (list.size() + 1));
                    if (position == list.size()) {
                        artRemarkTextView.setTextColor(Color.parseColor("#d9693565"));
                    } else {
                        artRemarkTextView.setTextColor(Color.parseColor("#ffffff"));
                    }


                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });


            LinearLayout backLayout = popupWindow_view.findViewById(R.id.ll_base_back);
            backLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mShowPicPopupWindow != null) {
                        mShowPicPopupWindow.dismiss();
                        mShowPicPopupWindow = null;
                    }
                }
            });

            TextView textView = popupWindow_view.findViewById(R.id.tv_base_header_title);
            textView.setText("查看详情");


            currentIndexTextView.setText((pos + 1) + "/" + (list.size() + 1));

        }
    }


    private List<ArtModel> mArtModelList = new ArrayList<>();
    private List<PullRecyclerMoreStatueModel> mCommonArtModelList = new ArrayList<>();
    private int mCurrentIndex = 0;
    private int mCurrentSize = 50;
    private int mType = 0;

    /**
     * @param type 0 加载推荐
     *             1 加载文章
     *             2 加载视频
     */
    public void loadingNetDatas(int type) {
        mType = type;

        /**
         * 已有数据，则不再去重复加载加载数据
         */
        if (mCommonArtModelList.size() > 0) {
            return;
        }

        if (mArtSqlDBHelper == null) {
            return;
        }

        if (mType == 0 || mType == 1) {
            ExecutorFunction.getInstance().addRunnableTask(new Runnable() {
                @Override
                public void run() {

                    List<ArtModel> artModels = mArtSqlDBHelper.queryArtModelList(mType);
                    if (artModels.size() > 0) {


                        for (int i = 0; i < artModels.size(); i++) {
                            ArtModel artModel = artModels.get(i);

                            String imageUrl = artModel.artImageUrl;
                            if (!TextUtils.isEmpty(imageUrl) && imageUrl.contains(",")) {

                                String[] imageUrlArray = imageUrl.split(",");
                                for (int i1 = 0; i1 < imageUrlArray.length; i1++) {
                                    artModel.artImageUrlList.add(imageUrlArray[i1]);
                                }

                            } else {
                                artModel.artImageUrlList.add(imageUrl);
                            }


                            PullRecyclerMoreStatueModel pullRecyclerMoreStatueModel = new PullRecyclerMoreStatueModel();
                            pullRecyclerMoreStatueModel.model = artModel;
                            if (artModel.artImageUrlList.size() != 1) {
                                pullRecyclerMoreStatueModel.itemLayoutId = R.layout.item_home_art_pic_layout;
                                pullRecyclerMoreStatueModel.itemType = 6;
                            } else {
                                pullRecyclerMoreStatueModel.itemLayoutId = R.layout.item_home_art_layout;
                                pullRecyclerMoreStatueModel.itemType = 1;
                            }


                            mCommonArtModelList.add(pullRecyclerMoreStatueModel);

                        }
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                pullRecyclerViewUtils.setLoadingDataList(mCommonArtModelList);
                            }
                        });

                    } else {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                loadNet(mType);
                            }
                        });

                    }
                }
            });
        } else {
            loadNet(mType);
        }

    }

    private void loadNet(int type) {

        pullRecyclerViewUtils.startLoadingAnimation();

        if (mType == 0 || mType == 1) {

            if (mType == 0) {
                type = 1;
            } else if (mType == 1) {
                type = 0;
            }

            HomeHttpManager.getInstance().getArtListFunction(type, mCurrentIndex, mCurrentSize, new HttpCallBackHelper<List<ArtModel>>() {
                @Override
                public void onSuccess(final List<ArtModel> artModels) {
                    LogUtils.d("加载成功 ... " + artModels.size());
                    pullRecyclerViewUtils.stopLoadingAnimation();

                    if (artModels != null && artModels.size() > 0) {


                        mArtModelList.addAll(artModels);

                        ExecutorFunction.getInstance().addRunnableTask(new Runnable() {
                            @Override
                            public void run() {

                                if (mNetLoadingType == NetLoadingType.PULL_LOADING) {
                                    //下拉刷新 清空数据库
                                    mArtSqlDBHelper.clearArtDb();
                                }
                                mArtSqlDBHelper.insertArtList(artModels);

                            }
                        });

                        for (int i = 0; i < artModels.size(); i++) {
                            ArtModel artModel = artModels.get(i);

                            String imageUrl = artModel.artImageUrl;

                            if (!TextUtils.isEmpty(imageUrl) && imageUrl.contains(",")) {

                                String[] imageUrlArray = imageUrl.split(",");
                                for (int i1 = 0; i1 < imageUrlArray.length; i1++) {
                                    artModel.artImageUrlList.add(imageUrlArray[i1]);
                                }

                            } else {
                                artModel.artImageUrlList.add(imageUrl);
                            }

                            PullRecyclerMoreStatueModel pullRecyclerMoreStatueModel = new PullRecyclerMoreStatueModel();
                            pullRecyclerMoreStatueModel.model = artModel;
                            if (artModel.artImageUrlList.size() != 1) {
                                pullRecyclerMoreStatueModel.itemLayoutId = R.layout.item_home_art_pic_layout;
                                pullRecyclerMoreStatueModel.itemType = 6;
                            } else {
                                pullRecyclerMoreStatueModel.itemLayoutId = R.layout.item_home_art_layout;
                                pullRecyclerMoreStatueModel.itemType = 1;
                            }


                            mCommonArtModelList.add(pullRecyclerMoreStatueModel);

                        }
                        pullRecyclerViewUtils.setLoadingDataList(mCommonArtModelList);

                    } else {
                        pullRecyclerViewUtils.setLoadingDataList(null);
                    }


                }

                @Override
                public void onFaile(String msg) {
                    LogUtils.e("加载失败");

                    pullRecyclerViewUtils.stopLoadingAnimation();
                    switch (mNetLoadingType) {
                        case PULL_LOADING:
                            mCommonArtModelList.clear();
                            break;
                        case UP_LOADING:
                            pullRecyclerViewUtils.setLoadingDataList(null);
                            return;
                        case DEFAULTlOADING:
                            mCommonArtModelList.clear();
                            break;
                    }

                    ExecutorFunction.getInstance().addRunnableTask(new Runnable() {
                        @Override
                        public void run() {

                            List<ArtModel> artModels = mArtSqlDBHelper.queryArtModelList(mType);
                            if (artModels.size() > 0) {
                                for (ArtModel artModel : artModels) {

                                    PullRecyclerMoreStatueModel pullRecyclerMoreStatueModel = new PullRecyclerMoreStatueModel();
                                    pullRecyclerMoreStatueModel.model = artModel;
                                    pullRecyclerMoreStatueModel.itemLayoutId = R.layout.item_home_art_layout;
                                    pullRecyclerMoreStatueModel.itemType = 1;

                                    mCommonArtModelList.add(pullRecyclerMoreStatueModel);

                                }
                                mHandler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        pullRecyclerViewUtils.setLoadingDataList(mCommonArtModelList);
                                    }
                                });

                            }
                        }
                    });
                }
            });

        } else if (mType == 2) {
            //视频
            HomeHttpManager.getInstance().getVideoListFunction(type, mCurrentIndex, mCurrentSize, new HttpCallBackHelper<List<VideoModel>>() {
                @Override
                public void onSuccess(final List<VideoModel> artModels) {
                    LogUtils.d("加载成功 ... " + artModels.size());
                    pullRecyclerViewUtils.stopLoadingAnimation();

                    if (artModels != null && artModels.size() > 0) {


//                        mArtModelList.addAll(artModels);
//
//                        ExecutorFunction.getInstance().addRunnableTask(new Runnable() {
//                            @Override
//                            public void run() {
//                                for (ArtModel artModel : artModels) {
//                                    mArtSqlDBHelper.insertArt(artModel);
//                                }
//                            }
//                        });

                        for (VideoModel artModel : artModels) {

                            PullRecyclerMoreStatueModel pullRecyclerMoreStatueModel = new PullRecyclerMoreStatueModel();
                            pullRecyclerMoreStatueModel.model = artModel;
                            pullRecyclerMoreStatueModel.itemLayoutId = R.layout.item_home_video_layout;
                            pullRecyclerMoreStatueModel.itemType = 2;

                            mCommonArtModelList.add(pullRecyclerMoreStatueModel);

                        }
                        pullRecyclerViewUtils.setLoadingDataList(mCommonArtModelList);

                    } else {
                        pullRecyclerViewUtils.setLoadingDataList(null);
                    }


                }

                @Override
                public void onFaile(String msg) {
                    LogUtils.e("加载失败");
                    pullRecyclerViewUtils.stopLoadingAnimation();

                    switch (mNetLoadingType) {
                        case PULL_LOADING:
                            mCommonArtModelList.clear();
                            break;
                        case UP_LOADING:
                            pullRecyclerViewUtils.setLoadingDataList(null);
                            return;
                        case DEFAULTlOADING:
                            mCommonArtModelList.clear();
                            break;
                    }

//                    ExecutorFunction.getInstance().addRunnableTask(new Runnable() {
//                        @Override
//                        public void run() {
//
//                            List<ArtModel> artModels = mArtSqlDBHelper.queryArtModelList(mType);
//                            if (artModels.size() > 0) {
//                                for (ArtModel artModel : artModels) {
//
//                                    PullRecyclerMoreStatueModel pullRecyclerMoreStatueModel = new PullRecyclerMoreStatueModel();
//                                    pullRecyclerMoreStatueModel.model = artModel;
//                                    pullRecyclerMoreStatueModel.itemLayoutId = R.layout.item_home_art_layout;
//                                    pullRecyclerMoreStatueModel.itemType = 1;
//
//                                    mCommonArtModelList.add(pullRecyclerMoreStatueModel);
//
//                                }
//                                mHandler.post(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        pullRecyclerViewUtils.setLoadingDataList(mCommonArtModelList);
//                                    }
//                                });
//
//                            }
//                        }
//                    });
                }
            });
        } else {
            //Android相关的文章与视频
            //根据PageType来加载数据
            //视频
            HomeHttpManager.getInstance().getArtAndVideoListFromNameFunction(mPageTape, mCurrentIndex, mCurrentSize, new HttpCallBackHelper<CommonSearcherModel>() {
                @Override
                public void onSuccess(final CommonSearcherModel commonSearcherModel) {

                    List<ArtModel> articleModels = commonSearcherModel.articleModels;
                    List<VideoModel> videoModels = commonSearcherModel.videoModels;

                    pullRecyclerViewUtils.stopLoadingAnimation();
                    if ((articleModels == null || articleModels.size() == 0) && (videoModels == null || videoModels.size() == 0)) {

                        pullRecyclerViewUtils.setLoadingDataList(null);
//                        mArtModelList.addAll(artModels);
//
//                        ExecutorFunction.getInstance().addRunnableTask(new Runnable() {
//                            @Override
//                            public void run() {
//                                for (ArtModel artModel : artModels) {
//                                    mArtSqlDBHelper.insertArt(artModel);
//                                }
//                            }
//                        });


                    } else {
                        if (videoModels != null || videoModels.size() > 0) {
                            for (VideoModel artModel : videoModels) {

                                PullRecyclerMoreStatueModel pullRecyclerMoreStatueModel = new PullRecyclerMoreStatueModel();
                                pullRecyclerMoreStatueModel.model = artModel;
                                pullRecyclerMoreStatueModel.itemLayoutId = R.layout.item_home_video_layout;
                                pullRecyclerMoreStatueModel.itemType = 3;

                                mCommonArtModelList.add(pullRecyclerMoreStatueModel);

                            }

                        }
                        if (articleModels != null || articleModels.size() > 0) {
                            for (ArtModel artModel : articleModels) {


                                String imageUrl = artModel.artImageUrl;
                                if (!TextUtils.isEmpty(imageUrl) && imageUrl.contains(",")) {

                                    String[] imageUrlArray = imageUrl.split(",");
                                    for (int i1 = 0; i1 < imageUrlArray.length; i1++) {
                                        artModel.artImageUrlList.add(imageUrlArray[i1]);
                                    }

                                } else {
                                    artModel.artImageUrlList.add(imageUrl);
                                }


                                PullRecyclerMoreStatueModel pullRecyclerMoreStatueModel = new PullRecyclerMoreStatueModel();
                                pullRecyclerMoreStatueModel.model = artModel;

                                if (artModel.artImageUrlList.size() != 1) {
                                    pullRecyclerMoreStatueModel.itemLayoutId = R.layout.item_home_art_pic_layout;
                                    pullRecyclerMoreStatueModel.itemType = 6;
                                } else {
                                    pullRecyclerMoreStatueModel.itemLayoutId = R.layout.item_home_art_layout;
                                    pullRecyclerMoreStatueModel.itemType = 4;
                                }


                                mCommonArtModelList.add(pullRecyclerMoreStatueModel);

                            }

                        }

                        pullRecyclerViewUtils.setLoadingDataList(mCommonArtModelList);
                    }


                }

                @Override
                public void onFaile(String msg) {
                    LogUtils.e("加载失败");
                    pullRecyclerViewUtils.stopLoadingAnimation();
                    pullRecyclerViewUtils.setLoadingDataList(null);
                    switch (mNetLoadingType) {
                        case PULL_LOADING:
                            mCommonArtModelList.clear();
                            break;
                        case UP_LOADING:

                            return;
                        case DEFAULTlOADING:
                            mCommonArtModelList.clear();
                            break;
                    }

//                    ExecutorFunction.getInstance().addRunnableTask(new Runnable() {
//                        @Override
//                        public void run() {
//
//                            List<ArtModel> artModels = mArtSqlDBHelper.queryArtModelList(mType);
//                            if (artModels.size() > 0) {
//                                for (ArtModel artModel : artModels) {
//
//                                    PullRecyclerMoreStatueModel pullRecyclerMoreStatueModel = new PullRecyclerMoreStatueModel();
//                                    pullRecyclerMoreStatueModel.model = artModel;
//                                    pullRecyclerMoreStatueModel.itemLayoutId = R.layout.item_home_art_layout;
//                                    pullRecyclerMoreStatueModel.itemType = 1;
//
//                                    mCommonArtModelList.add(pullRecyclerMoreStatueModel);
//
//                                }
//                                mHandler.post(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        pullRecyclerViewUtils.setLoadingDataList(mCommonArtModelList);
//                                    }
//                                });
//
//                            }
//                        }
//                    });
                }
            });
        }

    }
}
